#ifndef __FATFS_SD_H
#define __FATFS_SD_H

#include "stm32f4xx.h"

/* Definitions for MMC/SDC command */
#define CMD0     (0x40+0)     	/* GO_IDLE_STATE */
#define CMD1     (0x40+1)     	/* SEND_OP_COND */
#define CMD8     (0x40+8)     	/* SEND_IF_COND */
#define CMD9     (0x40+9)     	/* SEND_CSD */
#define CMD10    (0x40+10)    	/* SEND_CID */
#define CMD12    (0x40+12)    	/* STOP_TRANSMISSION */
#define CMD16    (0x40+16)    	/* SET_BLOCKLEN */
#define CMD17    (0x40+17)    	/* READ_SINGLE_BLOCK */
#define CMD18    (0x40+18)    	/* READ_MULTIPLE_BLOCK */
#define CMD23    (0x40+23)    	/* SET_BLOCK_COUNT */
#define CMD24    (0x40+24)    	/* WRITE_BLOCK */
#define CMD25    (0x40+25)    	/* WRITE_MULTIPLE_BLOCK */
#define CMD41    (0x40+41)    	/* SEND_OP_COND (ACMD) */
#define CMD55    (0x40+55)    	/* APP_CMD */
#define CMD58    (0x40+58)    	/* READ_OCR */

/* MMC card type flags (MMC_GET_TYPE) */
#define CT_MMC		0x01		/* MMC ver 3 */
#define CT_SD1		0x02		/* SD ver 1 */
#define CT_SD2		0x04		/* SD ver 2 */
#define CT_SDC		0x06		/* SD */
#define CT_BLOCK	0x08		/* Block addressing */

/* Functions 
DSTATUS SD_disk_initialize (uint8_t pdrv);
DSTATUS SD_disk_status (uint8_t pdrv);
DRESULT SD_disk_read (uint8_t pdrv, uint8_t* buff, DWORD sector, UINT count);
DRESULT SD_disk_write (uint8_t pdrv, const uint8_t* buff, DWORD sector, UINT count);
DRESULT SD_disk_ioctl (uint8_t pdrv, uint8_t cmd, void* buff);
*/

#define SPI_TIMEOUT 100

#define TF_CS_PORT						GPIOB
#define TF_CS							GPIO_Pin_7

#define FLASH_CS_PORT					GPIOB
#define FLASH_CS						GPIO_Pin_6

#define TF_PORT							GPIOB
#define TF_SCK							GPIO_Pin_3
#define TF_MISO							GPIO_Pin_4
#define TF_MOSI							GPIO_Pin_5

#define TF_SCK_SOURCE					GPIO_PinSource3
#define TF_MISO_SOURCE					GPIO_PinSource4
#define TF_MOSI_SOURCE					GPIO_PinSource5


#define SPI3_DMA                      	DMA1
#define SPI3_DMA_CLK                  	RCC_AHB1Periph_DMA1
#define SPI3_TX_DMA_CHANNEL           	DMA_Channel_0
#define SPI3_TX_DMA_STREAM            	DMA1_Stream5
#define SPI3_TX_DMA_FLAG_TCIF         	DMA_FLAG_TCIF5
#define SPI3_RX_DMA_CHANNEL           	DMA_Channel_0
#define SPI3_RX_DMA_STREAM            	DMA1_Stream2
#define SPI3_RX_DMA_FLAG_TCIF         	DMA_FLAG_TCIF2

#endif

#define STA_NOINIT		0x01	/* Drive not initialized */
#define STA_NODISK		0x02	/* No medium in the drive */
#define STA_PROTECT		0x04	/* Write protected */

/* Results of Disk Functions */
typedef enum {
	RES_OK = 0,		/* 0: Successful */
	RES_ERROR,		/* 1: R/W Error */
	RES_WRPRT,		/* 2: Write Protected */
	RES_NOTRDY,		/* 3: Not Ready */
	RES_PARERR		/* 4: Invalid Parameter */
} DRESULT;

/* Command code for disk_ioctrl fucntion */

/* Generic command (Used by FatFs) */
#define CTRL_SYNC			0	/* Complete pending write process (needed at FF_FS_READONLY == 0) */
#define GET_SECTOR_COUNT	1	/* Get media size (needed at FF_USE_MKFS == 1) */
#define GET_SECTOR_SIZE		2	/* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
#define GET_BLOCK_SIZE		3	/* Get erase block size (needed at FF_USE_MKFS == 1) */
#define CTRL_TRIM			4	/* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */

/* Generic command (Not used by FatFs) */
#define CTRL_POWER			5	/* Get/Set power status */
#define CTRL_LOCK			6	/* Lock/Unlock media removal */
#define CTRL_EJECT			7	/* Eject media */
#define CTRL_FORMAT			8	/* Create physical format on the media */

/* MMC/SDC specific ioctl command */
#define MMC_GET_TYPE		10	/* Get card type */
#define MMC_GET_CSD			11	/* Get CSD */
#define MMC_GET_CID			12	/* Get CID */
#define MMC_GET_OCR			13	/* Get OCR */
#define MMC_GET_SDSTAT		14	/* Get SD status */
#define ISDIO_READ			55	/* Read data form SD iSDIO register */
#define ISDIO_WRITE			56	/* Write data to SD iSDIO register */
#define ISDIO_MRITE			57	/* Masked write data to SD iSDIO register */

/* ATA/CF specific ioctl command */
#define ATA_GET_REV			20	/* Get F/W revision */
#define ATA_GET_MODEL		21	/* Get model name */
#define ATA_GET_SN			22	/* Get serial number */


void SdCard_init(void);

uint8_t SD_disk_initialize(uint8_t drv);
uint8_t SD_disk_status(uint8_t drv);
uint8_t SD_disk_read(uint8_t pdrv, uint8_t* buff, uint32_t sector, uint16_t count);
uint8_t SD_disk_write(uint8_t pdrv, const uint8_t* buff, uint32_t sector, uint16_t count);
uint8_t SD_disk_ioctl(uint8_t drv, uint8_t ctrl, void *buff);


